﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1;

namespace WebApplication2
{
    public partial class Login : System.Web.UI.Page
    {
        #region 初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Refresh();
            }
        }
        #endregion

        #region 封装刷新数据方法
        public void Refresh()
        {
            GridView1.DataSource = DbHelp.DataTable("select * from Girl");
            GridView1.DataBind();
        }
        #endregion

        #region 封装提取数据方法
        public string FindData(int rowID, int columeID)
        {
            var a = GridView1.Rows[rowID].Cells[columeID];
            var b = a.Controls.Count > 0 ? ((TextBox)a.Controls[0]).Text : a.Text;
            var c = GridView1.Rows[rowID].Cells[columeID].Controls.Count > 0 ? ((TextBox)GridView1.Rows[rowID].Cells[columeID].Controls[0]).Text : GridView1.Rows[rowID].Cells[columeID].Text;
            return b;
        }
        #endregion

        #region 删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView1.EditIndex = e.RowIndex;
            var id = FindData(GridView1.EditIndex, 0);
            var sql = string.Format("delete Girl where id={0} ", id);
            DbHelp.CMD(sql);
            GridView1.EditIndex = -1;
            Refresh();
        }
        #endregion

        #region 更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView1.EditIndex = e.RowIndex;
            var id = FindData(GridView1.EditIndex, 0);
            var name = FindData(GridView1.EditIndex, 1);
            var age = FindData(GridView1.EditIndex, 2);
            var height = FindData(GridView1.EditIndex, 3);
            var sql = string.Format("update Girl set name='{0}',age={1},height='{2}' where id={3}", name, age, height, id);
            DbHelp.CMD(sql);
            GridView1.EditIndex = -1;
            Refresh();
        }
        #endregion

        #region 取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Refresh();
        }
        #endregion

        #region 编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Refresh();
        }
        #endregion

    }
}